<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备编码" prop="deviceEncrypt">
      <el-input v-model="dataForm.deviceEncrypt" placeholder="设备编码"></el-input>
    </el-form-item>
    <el-form-item label="设备指标" prop="deviceCode">
      <el-input v-model="dataForm.deviceCode" placeholder="设备指标"></el-input>
    </el-form-item>
    <el-form-item label="实际值" prop="realValue">
      <el-input v-model="dataForm.realValue" placeholder="实际值"></el-input>
    </el-form-item>
    <el-form-item label="标准值" prop="quotaValue">
      <el-input v-model="dataForm.quotaValue" placeholder="标准值"></el-input>
    </el-form-item>
    <el-form-item label="超标标志位，hour_warning小时超标、day_warning24小时超标、year_warning年超标" prop="rangeEnum">
      <el-input v-model="dataForm.rangeEnum" placeholder="超标标志位，hour_warning小时超标、day_warning24小时超标、year_warning年超标"></el-input>
    </el-form-item>
    <el-form-item label="发生时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="发生时间"></el-input>
    </el-form-item>
    <el-form-item label="发生地点" prop="createAddress">
      <el-input v-model="dataForm.createAddress" placeholder="发生地点"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deviceEncrypt: '',
          deviceCode: '',
          realValue: '',
          quotaValue: '',
          rangeEnum: '',
          createTime: '',
          createAddress: ''
        },
        dataRule: {
          deviceEncrypt: [
            { required: true, message: '设备编码不能为空', trigger: 'blur' }
          ],
          deviceCode: [
            { required: true, message: '设备指标不能为空', trigger: 'blur' }
          ],
          realValue: [
            { required: true, message: '实际值不能为空', trigger: 'blur' }
          ],
          quotaValue: [
            { required: true, message: '标准值不能为空', trigger: 'blur' }
          ],
          rangeEnum: [
            { required: true, message: '超标标志位，hour_warning小时超标、day_warning24小时超标、year_warning年超标不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '发生时间不能为空', trigger: 'blur' }
          ],
          createAddress: [
            { required: true, message: '发生地点不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/devicewarningquota/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deviceEncrypt = data.deviceWarningQuota.deviceEncrypt
                this.dataForm.deviceCode = data.deviceWarningQuota.deviceCode
                this.dataForm.realValue = data.deviceWarningQuota.realValue
                this.dataForm.quotaValue = data.deviceWarningQuota.quotaValue
                this.dataForm.rangeEnum = data.deviceWarningQuota.rangeEnum
                this.dataForm.createTime = data.deviceWarningQuota.createTime
                this.dataForm.createAddress = data.deviceWarningQuota.createAddress
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/devicewarningquota/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deviceEncrypt': this.dataForm.deviceEncrypt,
                'deviceCode': this.dataForm.deviceCode,
                'realValue': this.dataForm.realValue,
                'quotaValue': this.dataForm.quotaValue,
                'rangeEnum': this.dataForm.rangeEnum,
                'createTime': this.dataForm.createTime,
                'createAddress': this.dataForm.createAddress
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
